package com.ruoyi.dataserver.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.dataserver.config.ScConfigAddress;
import com.ruoyi.dataserver.utils.ExcelExportUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName intelligent-city
 * @Package com.lab.dataserver.controller
 * @ClassName AssistDecisionController
 * @Description TODO
 * @Company Lab
 * @Author songhw
 * @Date 2020/8/5 0005 11:38
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/redReview")
@Slf4j
public class AssistDecisionController {

    @Autowired
    private ScConfigAddress scConfigAddress;

    @PostMapping("/export")
    public void export(@RequestBody JSONArray statisticDto, HttpServletResponse response) {

        List<String> list = new ArrayList();
        list.add("所在位置");
        list.add("管线类型");
        list.add("材质");
        list.add("权属单位");
        list.add("侵入长度");
        list.add("侵入面积");

        String deviceTemplate = scConfigAddress.getExcelExportFilePath();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String format = df.format(new Date());// new Date()为获取当前系统时间
        String destFilePath = deviceTemplate+"/"+format+".xlsx";
        BigExcelWriter writer = ExcelUtil.getBigWriter(destFilePath);
        ArrayList<Object> home = CollUtil.newArrayList(list.get(0),list.get(1),list.get(2),list.get(3),list.get(4),list.get(5));
        List homeCollection = new ArrayList();
        homeCollection.add(home);
        writer.write(homeCollection, true);
        for (int i = 0; i <statisticDto.size(); i++) {
            JSONObject jsonObject = JSON.parseObject(JSONObject.toJSONString(statisticDto.get(i)));
            List collection = new ArrayList();
            ArrayList<Object> objects = CollUtil.newArrayList(jsonObject.get("SZDL"),jsonObject.get("name"),jsonObject.get("CZ"),jsonObject.get("QSDW"),jsonObject.get("SMLENGTH"),jsonObject.get("QRMJ"));
            collection.add(objects);
            objects = CollUtil.newArrayList(collection);
            writer.write(objects, true);
        }
        writer.getSheet().setColumnWidth(3,3*1500);
//        writer.getSheet().setColumnWidth(4,4*2000);
        writer.close();
        if(destFilePath != null){
            try {
                ExcelExportUtils.downFile(destFilePath,response);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        ExcelExportUtils.delFile(destFilePath);
    }
}
